package com.sec.user.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.sec.base.utils.AjaxJson;
import com.sec.user.dao.UserDao;
import com.sec.user.entity.User;
import com.sec.user.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;

	@Override
	public List<User> findByUserNameAndPassword(User user) {
		return userDao.findByUserNameAndPassword(user.getUserName(), user.getPassword());
	}

	@Override
	public AjaxJson changePassword(HttpServletRequest request, String originalPassword, String newPassword,
			String confirmPassword) {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("修改密码失败");
		try {
			User user = (User) request.getSession().getAttribute("user");
			if (user == null) {
				j.setMsg("请登录");
				return j;
			}
			List<User> users = userDao.findByUserNameAndPassword(user.getUserName(), originalPassword);
			if (users.size() <= 0) {
				j.setMsg("原密码输入错误");
				return j;
			}
			if (!newPassword.equals(confirmPassword)) {
				j.setMsg("新密码与确认密码不一致");
				return j;
			}
			User u = users.get(0);
			u.setPassword(newPassword);
			userDao.save(u);
			j.setCode("success");
			j.setMsg("修改密码成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}

	@Override
	public AjaxJson findUserListByPage(int pageNumber, int pageSize) {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("获取分页数据失败");
		try {
			Pageable pageable = PageRequest.of(pageNumber - 1, pageSize);
			Page<User> page = userDao.findAll(pageable);
			Map<String, Object> pagaData = new HashMap<>();
			pagaData.put("userList", page.getContent());
			pagaData.put("count", page.getTotalElements());
			j.setData(pagaData);
			j.setCode("success");
			j.setMsg("获取分页数据成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}

	@Override
	public AjaxJson saveOrUpdate(User user) {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("操作失败");
		try {
			userDao.save(user);
			j.setCode("success");
			j.setMsg("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}

	@Override
	public AjaxJson getUserInfo(String id) {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("查询用户信息失败");
		Optional<User> optional = userDao.findById(id);
		if (optional.isPresent()) {
			User u = optional.get();
			j.setCode("success");
			j.setMsg("查询用户信息成功");
			j.setData(u);
		}
		return j;
	}

	@Override
	public AjaxJson deleteUser(String id) {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("删除用户失败");
		try {
			Optional<User> optional = userDao.findById(id);
			if (optional.isPresent()) {
				User u = optional.get();
				userDao.delete(u);
				j.setCode("success");
				j.setMsg("删除用户成功");
			} else {
				j.setMsg("删除用户数据异常");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}
}
